package com.zhiche.lisa.lspm.controller;

import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.lspm.service.upload.UploadService;
import com.zhiche.lisa.lspm.vo.attach.AttachInfoVO;
import com.zhiche.lisa.lspm.vo.attach.AttachKeyVO;
import com.zhiche.lisa.lspm.vo.common.GetUrlVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 上传 接口
 * @since 2018-06-01
 * @author lbl
 */
@RestController
@RequestMapping("/upload")
@Api(value = "/upload", description = "上传接口",tags={"上传接口"})
public class UploadController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);

    @Autowired
    private UploadService uploadService;

    @GetMapping(value = "/getToken")
    @ApiOperation(value = "获取token", notes = "获取token", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<String> getToken(String key) {
        LOGGER.info("UploadController.getToken（获取 token） params : {} ");
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(),CommonEnum.SUCCESS.getText(),null);
        try {
            String token = uploadService.getToken(key);
            result.setData(token);
        } catch (BaseException e) {
            LOGGER.error("UploadController.getToken ERROR Message : {} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("UploadController.getToken ERROR Message : {} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/getUrl")
    @ApiOperation(value = "获取下载Url", notes = "获取下载Url", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<GetUrlVO> getUrl(String key,String type) {
        LOGGER.info("UploadController.getUrl（获取 Url） params : {} ");

        RestfulResponse<GetUrlVO> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(),CommonEnum.SUCCESS.getText(),null);

        try {
            result.setData(uploadService.getUrl(key,type));
        } catch (BaseException e) {
            LOGGER.error("UploadController.getUrl ERROR Message : {} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("UploadController.getUrl ERROR Message : {} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }


    @PostMapping(value = "/getAttachInfo")
    @ApiOperation(value = "获取附件信息", notes = "获取附件信息", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<List<AttachInfoVO>> getAttachInfo(@RequestBody AttachKeyVO attachKeyVO) {
        LOGGER.info("UploadController.getUrl（获取 Url） params : {} ");

        RestfulResponse<List<AttachInfoVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(),CommonEnum.SUCCESS.getText(),null);

        try {
            result.setData(uploadService.getAttachInfo(attachKeyVO));
        } catch (BaseException e) {
            LOGGER.error("UploadController.getAttachInfo ERROR Message : {} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("UploadController.getAttachInfo ERROR Message : {} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }


    @GetMapping(value = "/getDownLoadUrl")
    @ApiOperation(value = "获取下载Url", notes = "获取下载Url", produces = MediaType.ALL_VALUE,response = RestfulResponse.class)
    public RestfulResponse<String> getDownLoadUrl(String key) {
        LOGGER.info("UploadController.getUrl（获取 Url） params : {} ");
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(),CommonEnum.SUCCESS.getText(),null);
        try {
            result.setData(uploadService.getStrUrl(key));
        } catch (BaseException e) {
            LOGGER.error("UploadController.getUrl ERROR Message : {} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("UploadController.getUrl ERROR Message : {} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }
}
